<?php

	namespace application\controller;

		require_once ("application/view/RecipeCollectionView.php");
		require_once ("application/model/RecipeCollectionModel.php");
		require_once ("application/model/RecipeDALmodel.php");

		class AdminController
		{

			private $recipeCollectionView;
			private $recipeCollection;
			private $message;
			private $recipe;
			private $recipeId;

			//tar in en recipeCollectionLista och skickar den vidare till vyn
			public function __construct(\application\model\RecipeCollectionModel $recipeCollection)
			{
				$this->recipeCollection = $recipeCollection;
				$this->recipeCollectionView = new \application\view\RecipeCollectionView($this->recipeCollection);
				$this->loginController = new \login\controller\LoginController();
			}


			public function viewAdminPage()
			{

				//HÄR KRÄVS INLOGGNING
				//Denna controllern frågar logincontrollern om vi är inloggade
				if ($this->loginController->isUserLoggedIn() == true)
				{
					//ADMIN////////////////////////
					if ($this->recipeCollectionView->userWantsToWorkInAdminSection() == true)
					{
						return $this->recipeCollectionView->getAdminSectionAddRecipe();
					}

					//ADD
					if ($this->recipeCollectionView->userWantsToAddRecipe() == true)
					{
						try
						{
							$this->recipe = $this->recipeCollectionView->getNewRecipe();
							//Om nytt recept är skapat
							if ($this->recipe)
							{
								//När receptet är ok sparas det genom recipeCollection via DAL
								$this->recipeCollection->addRecipeToRecipeCollection($this->recipe);
								$latestRecipe = $this->recipeCollection->getLatestAddedRecipe();
								return $this->recipeCollectionView->newRecipeAddedSuccess($latestRecipe);
							}
							return $this->recipeCollectionView->newRecipeAddedFail();
						}
						catch (Exception $e)
						{
							return $this->recipeCollectionView->newRecipeAddedFail();
						}
					}
					
					//ADD IMAGE 
					
					if($this->recipeCollectionView->userWantsToAddImage() == true)
					{
						echo "ADD ME";
						
					}

					//UPDATE
					if ($this->recipeCollectionView->userWantsToUpdateRecipe() == true)
					{
						$this->recipeId = $this->recipeCollectionView->userWantsToUpdateThisRecipe();
						$this->recipe = $this->recipeCollection->getSingleRecipe($this->recipeId);
						return $this->recipeCollectionView->getEditFormPage($this->recipe);
					}

					if ($this->recipeCollectionView->userWantsToSaveUpdatedRecipe() == true)
					{
						$this->recipeId = $this->recipeCollectionView->userWantsToUpdateThisRecipe();
						$this->recipe = $this->recipeCollection->getSingleRecipe($this->recipeId);
						$updatedRecipe = $this->recipeCollectionView->getEditedRecipe($this->recipe);
						$this->recipeCollection->updateRecipeInRecipeCollection($updatedRecipe);
						$updatedRecipe = $this->recipeCollection->getSingleRecipe($this->recipeId);
						return $this->recipeCollectionView->recipeUpdatedSuccess($updatedRecipe);
					}

					//DELETE
					if ($this->recipeCollectionView->userWantsToDeleteRecipe() == true)
					{
						$this->recipeId = $this->recipeCollectionView->userWantsToDeleteThisRecipe();
						return $this->recipeCollectionView->getDeletionConfirmation($this->recipeId);
					}
					if ($this->recipeCollectionView->userIsSureAboutDeletion() == true)
					{
						$this->recipeId = $this->recipeCollectionView->userWantsToDeleteThisRecipe();
						$this->recipeCollection->deleteRecipeFromCollection($this->recipeId);
						return $this->recipeCollectionView->recipeDeletedSuccessPage();
					}
					if ($this->recipeCollectionView->userAbortsDeletion() == true)
					{
						return $this->recipeCollectionView->recipeDeletedFail();
					}
					return $this->recipeCollectionView->getRecipeCollectionListHTML();

				}
				
			}


		}
